package cn.s1995.sloth.bms.controller;

import java.util.List;
import java.util.Objects;

import org.springdoc.core.annotations.ParameterObject;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.s1995.sloth.bizs.entity.OrderRefund;
import cn.s1995.sloth.bizs.oo.req.OrderRefundVerifyREQ;
import cn.s1995.sloth.bizs.service.OrderRefundService;
import cn.s1995.sloth.common.core.util.R;
import cn.s1995.sloth.common.excel.annotation.ResponseExcel;
import cn.s1995.sloth.common.log.annotation.SysLog;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

/**
 * 订单退款
 *
 * @author Lex
 * @date 2023-12-29 03:07:41
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/orderRefund")
@Tag(description = "OrderRefund", name = "BMS-订单退款管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class OrderRefundController {

    private final OrderRefundService orderRefundService;

    /**
     * 获取分页
     * @param page 分页对象
     * @param req 订单退款
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取分页", description = "获取分页")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('bizs_OrderRefund_view')")
    public R getPage(@ParameterObject Page page, @ParameterObject OrderRefund req) {
        LambdaQueryWrapper<OrderRefund> wrapper = Wrappers.lambdaQuery();
        //关键字
        wrapper.and(
                StrUtil.isNotEmpty(req.getSearch()),
                w -> w.like(OrderRefund::getOrderId, req.getSearch())
                        .or()
                        .like(OrderRefund::getFlowTradeNo, req.getSearch())
                        .or()
                        .like(OrderRefund::getOutRefundNo, req.getSearch())
        );
        //其他
		wrapper.eq(Objects.nonNull(req.getShopId()), OrderRefund::getShopId, req.getShopId());
		wrapper.eq(Objects.nonNull(req.getPayMethod()), OrderRefund::getPayMethod, req.getPayMethod());
		wrapper.eq(Objects.nonNull(req.getState()), OrderRefund::getState, req.getState());
		wrapper.eq(Objects.nonNull(req.getProcessState()), OrderRefund::getProcessState, req.getProcessState());
		wrapper.eq(Objects.nonNull(req.getApplyType()), OrderRefund::getApplyType, req.getApplyType());
        if(req.hasRangeTime()) {
        	wrapper.between(OrderRefund::getCreateTime, req.getRangeTime()[0], req.getRangeTime()[1]);
        }
        return R.ok(orderRefundService.page(page, wrapper));
    }

    /**
     * 获取列表
     * @param req 订单退款
     * @return
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取列表", description = "获取列表")
    @GetMapping("/list")
    @PreAuthorize("@pms.hasPermission('bizs_OrderRefund_view')")
    public R getList(@ParameterObject OrderRefund req) {
        LambdaQueryWrapper<OrderRefund> wrapper = Wrappers.lambdaQuery();
		wrapper.eq(Objects.nonNull(req.getUid()), OrderRefund::getUid, req.getUid());
		wrapper.eq(Objects.nonNull(req.getShopId()), OrderRefund::getShopId, req.getShopId());
		wrapper.eq(Objects.nonNull(req.getOrderId()), OrderRefund::getOrderId, req.getOrderId());
		wrapper.eq(Objects.nonNull(req.getPayMethod()), OrderRefund::getPayMethod, req.getPayMethod());
		wrapper.eq(StrUtil.isNotBlank(req.getFlowTradeNo()), OrderRefund::getFlowTradeNo, req.getFlowTradeNo());
		wrapper.eq(StrUtil.isNotBlank(req.getOutRefundNo()), OrderRefund::getOutRefundNo, req.getOutRefundNo());
		wrapper.eq(Objects.nonNull(req.getState()), OrderRefund::getState, req.getState());
		wrapper.eq(Objects.nonNull(req.getProcessState()), OrderRefund::getProcessState, req.getProcessState());
		wrapper.eq(Objects.nonNull(req.getApplyType()), OrderRefund::getApplyType, req.getApplyType());
        return R.ok(orderRefundService.list(wrapper));
    }

    /**
     * 获取对象
     * @param id id
     * @return R
     */
	@ApiOperationSupport(order = 1)
    @Operation(summary = "获取对象", description = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('bizs_OrderRefund_view')")
    public R getById(@PathVariable("id" ) Long id) {
        return R.ok(orderRefundService.getById(id));
    }

    /**
     * 新增订单退款
     * @param req 订单退款
     * @return R
     */
	@ApiOperationSupport(order = 10)
    @Operation(summary = "新增订单退款", description = "新增订单退款")
    @SysLog("新增订单退款")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('bizs_OrderRefund_add')")
    public R save(@RequestBody OrderRefund req) {
        return R.ok(orderRefundService.save(req));
    }

    /**
     * 审核订单退款
     * @param req 订单退款
     * @return R
     */
	@Deprecated
//	@ApiOperationSupport(order = 15)
//    @Operation(summary = "审核订单退款申请", description = "审核订单退款申请")
//    @SysLog("审核订单退款")
//    @PutMapping("/verify")
    @PreAuthorize("@pms.hasPermission('bizs_OrderRefund_edit')")
    public R verify(@RequestBody OrderRefundVerifyREQ req) {
		orderRefundService.verify(req);
		return R.ok();
    }

    /**
     * 修改订单退款
     * @param req 订单退款
     * @return R
     */
	@ApiOperationSupport(order = 20)
    @Operation(summary = "修改订单退款", description = "修改订单退款")
    @SysLog("修改订单退款")
    @PutMapping("/update")
    @PreAuthorize("@pms.hasPermission('bizs_OrderRefund_edit')")
    public R updateById(@RequestBody OrderRefund req) {
        return R.ok(orderRefundService.updateById(req));
    }

    /**
     * 通过id删除订单退款
     * @param ids id列表
     * @return R
     */
	@ApiOperationSupport(order = 30)
    @Operation(summary = "通过id删除订单退款", description = "通过id删除订单退款")
    @SysLog("通过id删除订单退款")
    @DeleteMapping
    @PreAuthorize("@pms.hasPermission('bizs_OrderRefund_del')")
    public R remove(@RequestBody Long[] ids) {
        return R.ok(orderRefundService.removeBatchByIds(CollUtil.toList(ids)));
    }

    /**
     * 导出excel 表格
     * @param req 查询条件
   	 * @param ids 导出指定ID
     * @return excel 文件流
     */
	@ApiOperationSupport(order = 40)
    @Operation(summary = "导出excel表格", description = "")
    @ResponseExcel
    @GetMapping("/export")
    @PreAuthorize("@pms.hasPermission('bizs_OrderRefund_export')")
    public List<OrderRefund> export(OrderRefund req, Long[] ids) {
        return orderRefundService.list(Wrappers.lambdaQuery(req).in(ArrayUtil.isNotEmpty(ids), OrderRefund::getId, ids));
    }
}
